import { defineStore } from 'pinia'

/**
 * 进度跟踪状态管理
 * 存储用户的数据跟踪、变化趋势等
 */
export const useProgressStore = defineStore('progress', {
  state: () => ({
    // 体重记录 (最近30天)
    weightLog: [
      { date: '2023-09-01', weight: 75.2 },
      { date: '2023-09-03', weight: 74.8 },
      { date: '2023-09-05', weight: 74.7 },
      { date: '2023-09-07', weight: 74.3 },
      { date: '2023-09-09', weight: 74.0 },
      { date: '2023-09-11', weight: 73.8 },
      { date: '2023-09-13', weight: 73.5 },
      { date: '2023-09-15', weight: 73.4 },
      { date: '2023-09-17', weight: 73.0 },
      { date: '2023-09-19', weight: 72.8 },
      { date: '2023-09-21', weight: 72.5 },
      { date: '2023-09-23', weight: 72.3 },
      { date: '2023-09-25', weight: 72.0 },
      { date: '2023-09-27', weight: 71.8 },
      { date: '2023-09-29', weight: 71.5 },
      { date: '2023-10-01', weight: 71.3 }
    ],
    
    // 目标体重
    targetWeight: 68.0,
    
    // 起始体重
    startWeight: 75.2,
    
    // 卡路里摄入记录 (每周)
    caloriesLog: [
      { week: '第1周', intake: 2150, burned: 520 },
      { week: '第2周', intake: 2050, burned: 550 },
      { week: '第3周', intake: 1980, burned: 580 },
      { week: '第4周', intake: 1920, burned: 600 }
    ],
    
    // 身体围度记录
    measurements: [
      { 
        date: '2023-09-01', 
        chest: 98, 
        waist: 87, 
        hips: 102, 
        thigh: 56 
      },
      { 
        date: '2023-09-15', 
        chest: 97, 
        waist: 86, 
        hips: 101, 
        thigh: 55.5 
      },
      { 
        date: '2023-10-01', 
        chest: 96, 
        waist: 84.5, 
        hips: 100, 
        thigh: 55 
      }
    ],
    
    // 完成签到的日期
    checkInDates: [
      '2023-09-25',
      '2023-09-26',
      '2023-09-27',
      '2023-09-28',
      '2023-09-29',
      '2023-09-30',
      '2023-10-01',
      '2023-10-02'
    ]
  }),
  
  getters: {
    // 获取当前体重
    currentWeight: (state) => {
      if (state.weightLog.length === 0) return 0
      return state.weightLog[state.weightLog.length - 1].weight
    },
    
    // 计算体重变化趋势（负数表示减轻，正数表示增加）
    weightTrend: (state) => {
      if (state.weightLog.length < 2) return 0
      const lastWeight = state.weightLog[state.weightLog.length - 1].weight
      const previousWeight = state.weightLog[state.weightLog.length - 2].weight
      return +(lastWeight - previousWeight).toFixed(1)
    },
    
    // 计算距离目标体重还有多少公斤
    remainingWeight: (state) => {
      if (state.weightLog.length === 0) return state.targetWeight - state.startWeight
      const currentWeight = state.weightLog[state.weightLog.length - 1].weight
      return +(currentWeight - state.targetWeight).toFixed(1)
    },
    
    // 计算总体减重量
    totalWeightLoss: (state) => {
      if (state.weightLog.length === 0) return 0
      const currentWeight = state.weightLog[state.weightLog.length - 1].weight
      return +(state.startWeight - currentWeight).toFixed(1)
    },
    
    // 计算减重进度百分比
    weightLossProgress: (state) => {
      if (state.weightLog.length === 0) return 0
      const currentWeight = state.weightLog[state.weightLog.length - 1].weight
      const totalToLose = state.startWeight - state.targetWeight
      const lost = state.startWeight - currentWeight
      return Math.round((lost / totalToLose) * 100)
    },
    
    // 获取腰围变化趋势
    waistTrend: (state) => {
      if (state.measurements.length < 2) return 0
      const lastMeasurement = state.measurements[state.measurements.length - 1].waist
      const previousMeasurement = state.measurements[state.measurements.length - 2].waist
      return +(lastMeasurement - previousMeasurement).toFixed(1)
    },
    
    // 当前连续签到天数
    currentStreak: (state) => {
      if (state.checkInDates.length === 0) return 0
      
      let streak = 1
      const today = new Date()
      today.setHours(0, 0, 0, 0)
      
      // 检查是否今天已签到
      const todayStr = today.toISOString().split('T')[0]
      const hasTodayCheckIn = state.checkInDates.includes(todayStr)
      
      if (!hasTodayCheckIn) {
        // 检查昨天是否签到
        const yesterday = new Date(today)
        yesterday.setDate(yesterday.getDate() - 1)
        const yesterdayStr = yesterday.toISOString().split('T')[0]
        
        if (!state.checkInDates.includes(yesterdayStr)) {
          return 0 // 今天和昨天都没签到，连续签到为0
        }
      }
      
      // 计算连续天数
      let checkDay = new Date(today)
      
      // 如果今天没签到，从昨天开始计算
      if (!hasTodayCheckIn) {
        checkDay.setDate(checkDay.getDate() - 1)
      }
      
      while (true) {
        // 检查前一天
        checkDay.setDate(checkDay.getDate() - 1)
        const checkDayStr = checkDay.toISOString().split('T')[0]
        
        if (state.checkInDates.includes(checkDayStr)) {
          streak++
        } else {
          break
        }
      }
      
      return streak
    }
  },
  
  actions: {
    // 添加体重记录
    addWeightRecord(weight) {
      const today = new Date().toISOString().split('T')[0]
      
      // 检查今天是否已有记录
      const existingIndex = this.weightLog.findIndex(log => log.date === today)
      
      if (existingIndex !== -1) {
        // 更新今天的记录
        this.weightLog[existingIndex].weight = weight
      } else {
        // 添加新记录
        this.weightLog.push({
          date: today,
          weight
        })
      }
      
      // 保持记录按日期排序
      this.weightLog.sort((a, b) => new Date(a.date) - new Date(b.date))
    },
    
    // 添加身体围度记录
    addMeasurement(measurement) {
      const today = new Date().toISOString().split('T')[0]
      
      // 检查今天是否已有记录
      const existingIndex = this.measurements.findIndex(m => m.date === today)
      
      if (existingIndex !== -1) {
        // 更新今天的记录
        this.measurements[existingIndex] = {
          ...this.measurements[existingIndex],
          ...measurement
        }
      } else {
        // 添加新记录
        this.measurements.push({
          date: today,
          ...measurement
        })
      }
      
      // 保持记录按日期排序
      this.measurements.sort((a, b) => new Date(a.date) - new Date(b.date))
    },
    
    // 添加签到记录
    checkIn() {
      const today = new Date().toISOString().split('T')[0]
      
      // 检查今天是否已签到
      if (!this.checkInDates.includes(today)) {
        this.checkInDates.push(today)
      }
    },
    
    // 添加卡路里记录
    addCaloriesRecord(week, intake, burned) {
      // 检查是否已有该周记录
      const existingIndex = this.caloriesLog.findIndex(log => log.week === week)
      
      if (existingIndex !== -1) {
        // 更新记录
        this.caloriesLog[existingIndex].intake = intake
        this.caloriesLog[existingIndex].burned = burned
      } else {
        // 添加新记录
        this.caloriesLog.push({
          week,
          intake,
          burned
        })
      }
    },
    
    // 设置目标体重
    setTargetWeight(weight) {
      this.targetWeight = weight
    }
  }
}) 